package org.tong.activiti6.test.utils;

import org.activiti.engine.repository.Deployment;
import org.springframework.util.ObjectUtils;

import javax.lang.model.type.DeclaredType;
import javax.swing.text.View;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tong
 * @date 2022-07-22 14:58
 */
public class ViewFormat {
    public static final String format(Object obj) {
        Class<?> clazz = obj.getClass();
        // 检索Method
        Map<String, Method> methodMap = new HashMap<>();
        Arrays.stream(clazz.getMethods()).forEach(method -> {
            methodMap.put(method.getName().toLowerCase(), method);
        });
        // 准备输出
        return clazz.getSimpleName() + "[" + Arrays.stream(clazz.getDeclaredFields()).map(field -> {
            String methodName = "get" + field.getName();
            Method method = methodMap.getOrDefault(methodName, null);
            if (!ObjectUtils.isEmpty(method)) {
                try {
                    return field.getName() + ":" + method.invoke(obj) + ",\t";
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            } else {
                return "";
            }
        }).collect(Collectors.joining()) + "]";
    }


    public static void main(String[] args) {
        System.out.println(ViewFormat.class.getCanonicalName());
    }

}
